package com.cupcorn.lib.game;

import java.io.IOException;

import org.andengine.audio.music.Music;
import org.andengine.audio.music.MusicFactory;

import com.cupcorn.lib.Utils;
import com.cupcorn.lib.define.GameEngineConfiguration;
import com.cupcorn.lib.game.element.GameElementMusic;

public class GameMusic
{
	Music	music;
	boolean	paused;
	
	public GameMusic(GameEngine engine, GameElementMusic gameElementMusic) throws IOException
	{
		paused = false;
		music = MusicFactory.createMusicFromAsset(engine.getMusicManager(), engine, gameElementMusic.path);
		music.setLooping(gameElementMusic.looping);
	}
	
	public void play()
	{
		if (!GameEngineConfiguration.use_music) return;
		paused = false;
		music.play();
	}
	
	public void pause()
	{
		if (!GameEngineConfiguration.use_music) return;
		paused = true;
		music.pause();
	}
	
	public void stop()
	{
		if (!GameEngineConfiguration.use_music) return;
		paused = false;
		music.pause();
		music.seekTo(0);
	}
	
	public void resume()
	{
		if (!GameEngineConfiguration.use_music) return;
		if (paused)
		{
			paused = false;
			music.play();
		}
		else
		{
			Utils.TRACE("cannot resume music...");
		}
	}
}
